﻿/*
 
Celerity: Sensory Overload
Copright 2012 by Adam Hill, David Gannon, Thomas Tamblyn & Patrick Ytting
Contact: adam.hill@outlook.com
Website: adamhill.org
Licence: MS-Pl Licence
  
 */

using Celerity.UI.Definition;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Celerity.Entities
{
    /// <summary>
    /// This represents a logical collection of UIEntityItems,
    /// which go to make up a single composite UI element
    /// e.g. a panel with some text and an icon
    /// </summary>
    public class UIEntity
    {
        public event EventHandler OnClicked;
        void RaiseOnClicked()
        {
            if (OnClicked != null)
            {
                if (DrawCondition == null || DrawCondition.IsValid)
                {
                    OnClicked(this, new EventArgs());
                }
            }
        }

        Vector2 offset = Vector2.Zero;
        Vector2 size = Vector2.Zero;
        Rectangle thisRect = new Rectangle();
        Rectangle touchRect = new Rectangle(1, 1, 1, 1);
        public UIDrawCondition DrawCondition { get; set; }
        public bool IsPressable { get; set; }
        public bool IsPressed { get; set; } // IsPressed is purely for visuals
        public bool HasUnprocessedTouch { get; set; }
        public Vector2 Offset { get { return offset; } set { offset = value; RecalcRect(); } }
        public Vector2 Size { get { return size; } set { size = value; RecalcRect(); } }
        public List<UIEntityItem> Items { get; set; }

        public virtual void ConfigureSelf()
        {

        }

        public void Update(IEnumerable<Vector2> interactions)
        {
            if (Items == null || !Items.Any())
            {
                return;
            }

            HandleClicksAndTouches(interactions);

            for (int i = 0; i < Items.Count(); i++)
            {
                var item = Items[i];
                item.IsPressed = IsPressed;
                item.Update();
            }
        }

        void HandleClicksAndTouches(IEnumerable<Vector2> interactions)
        {
            bool currentlyPressed = false;
            int touchCount = interactions.Count();
            if (touchCount > 0)
            {
                foreach(var interaction in interactions)
                {
                    bool thisTouched;
                    touchRect.X = (int)interaction.X;
                    touchRect.Y = (int)interaction.Y;
                    thisTouched = thisRect.Intersects(touchRect);
                    if (thisTouched)
                    {
                        currentlyPressed = true;
                    }
                }
            }
            else
            {
                touchRect.X = 0;
                touchRect.Y = 0;
            }

            if (IsPressed && !currentlyPressed)
            {
                RaiseOnClicked();
            }

            IsPressed = currentlyPressed;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Items == null || (DrawCondition != null && !DrawCondition.IsValid))
            {
                return;
            }

            for (int i = 0; i < Items.Count(); i++)
            {
                Items[i].Draw(spriteBatch);
            }
        }

        void RecalcRect()
        {
            thisRect.X = (int)offset.X;
            thisRect.Y =  (int)offset.Y;
            thisRect.Width = (int)size.X;
            thisRect.Height = (int)size.Y;
        }
    }
}
